//
//  MapViewController.m
//  CoreDataAddressBook
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MapViewController.h"

@interface MapViewController ()<BMKMapViewDelegate, BMKGeneralDelegate, BMKGeoCodeSearchDelegate, UIAlertViewDelegate>
@property(nonatomic, strong)BMKMapView *mapView;
@property(nonatomic, strong)BMKMapManager *mapManager;
@property(nonatomic, strong)BMKGeoCodeSearch *geocodersearch;
@property(nonatomic, assign)BOOL isGeoSearch;



@end

@implementation MapViewController
- (void)viewWillAppear:(BOOL)animated {
    [self.mapView viewWillAppear];
    self.mapView.delegate = self;
//    self.geocodersearch.delegate = self;
    
    
    
}



- (void)viewWillDisappear:(BOOL)animated {
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil;
    self.geocodersearch.delegate = nil;
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.mapManager = [[BMKMapManager alloc] init];
    BOOL ret = [self.mapManager start:@"0oXmxdsTx64BWVXzn54McDGn" generalDelegate:self];
    if (!ret) {
        NSLog(@"联网失败");
    }
    
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 7.0) {
        self.navigationController.navigationBar.translucent = NO;
    }
    
    
    self.mapView = [[BMKMapView alloc] initWithFrame:self.view.frame];
    self.mapView.mapType = BMKMapTypeStandard;
    [self.view addSubview:self.mapView];
    

//    [self action];
  
}



- (void)action {
    self.geocodersearch = [[BMKGeoCodeSearch alloc] init];
    self.geocodersearch.delegate = self;
    BMKGeoCodeSearchOption *geocodeSearchOption = [[BMKGeoCodeSearchOption alloc] init];
    geocodeSearchOption.address = self.address;
    BOOL flag = [self.geocodersearch geoCode:geocodeSearchOption];
    if (flag) {
        NSLog(@"geo检索成功");
    } else {
        NSLog(@"geo检索失败");
    }
}



#pragma mark - BMKMapViewDelegate
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"BMKMapView控件初始化完成" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
    [alert show];
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [self action];
    }
}



- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate {
    NSLog(@"map view: click blank");
}



- (void)mapview:(BMKMapView *)mapView onDoubleClick:(CLLocationCoordinate2D)coordinate {
    NSLog(@"map view: double click");
}



//根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation {
    NSString *AnnotationViewID = @"annotationViewID";
    //根据指定标识查找一个可被复用的标注View，一般在delegate中使用，用此函数来代替新申请一个View
    BMKAnnotationView *annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil) {
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
        ((BMKPinAnnotationView*)annotationView).animatesDrop = YES;
    }
    
    annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));
    annotationView.annotation = annotation;
    annotationView.canShowCallout = TRUE;
    return annotationView;
}



- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == 0) {
        BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
        item.coordinate = result.location;
        item.title = result.address;
        [_mapView addAnnotation:item];
        [self.mapView setCenterCoordinate:result.location animated:YES];
        
    }

}
























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
